package com.wondersgroup.wuhan_demo;

import android.app.Application;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.wondersgroup.wuhan_demo.entity.DaoSession;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import cn.mrobot.core.log.LogUtils;
import cn.mrobot.pkgcore.cookyplus.barcodescanner.BarcodeScanner;
import cn.mrobot.pkgcore.cookyplus.barcodescanner.BarcodeScannerInitListener;
import cn.mrobot.pkgcore.core.ErrorCode;
import cn.mrobot.pkgcore.core.PkgCore;
import cn.mrobot.pkgcore.core.kernel.MRobotInitListener;

import static com.wondersgroup.wuhan_demo.Constant.DB_NAME;

/**
 * @author by Xiong on 2017/7/31 14:21
 * @version 1.0.0
 * @since 1.0.0
 */
public class MyApplication extends Application {

    public static String doctorName = "";
    public static String deptName = "";

    private static final String TAG = MyApplication.class.getSimpleName();

    //APP_KEY
    private static final String APP_KEY = "2Sw6u939";

    //Face++ 演示ApiKey
    private static final String FACE_APP_KEY = "xokK8G89ysSB5LbeQEl769IVhlwjO1h3";
    //Face++ 演示ApiSecret
    private static final String FACE_APP_SECRET = "OkJg_zgQpPOd9pCdNVti6v_GJ-A2aZUo";
    public  static MyApplication instance=null;
    private static final String SYNC_LOCK = "SYNC_LOCK";
    DaoSession daoSession;
    protected static Context mContext;
    public BarcodeScanner mBarcodeScanner;
    public static boolean isScanInit=false;
    @Override
    public void onCreate() {
        super.onCreate();
        mContext=this;
        //初始化PkgCore
        //参数1: applicationContext
        //参数2: 是否初始化ROS连接
        //参数3: 是否初始化IPC连接
        mBarcodeScanner = BarcodeScanner.get();
        PkgCore.with(this).config(APP_KEY, true, true).initialize(new MRobotInitListener() {
            @Override
            public void onInitSucceed() {
                LogUtils.i("ssssssssssssss");
//                Toast.makeText(MyApplication.this, "onInitialized MRobot SDK 初始化成功",
//                        Toast.LENGTH_LONG).show();
                LogUtils.e(TAG, "MRobot onInitialized");
//                BarcodeScanner.get().init(null);

                mBarcodeScanner.init(new BarcodeScannerInitListener() {
                    @Override
                    public void onInitSuccess() {
//                handler.sendEmptyMessageDelayed(SCANCODE, 1000);
                        isScanInit=true;
                        Log.i(TAG, "onInitSuccess: 条码扫描仪初始化成功!");
                    }

                    @Override
                    public void onInitFail(ErrorCode errorCode) {
                        Log.i(TAG, "onInitFail: 条码扫描仪初始化失败: " + errorCode.getCode() + " , " + errorCode.getMessage());
                    }
                });
            }

            @Override
            public void onInitFailed(ErrorCode errorCode) {
                LogUtils.i("fffffffffffffffff");
                mBarcodeScanner.init(new BarcodeScannerInitListener() {
                    @Override
                    public void onInitSuccess() {
                        isScanInit=true;
//                handler.sendEmptyMessageDelayed(SCANCODE, 1000);
                        Log.i(TAG, "onInitSuccess: 条码扫描仪初始化成功!");
                    }

                    @Override
                    public void onInitFail(ErrorCode errorCode) {
                        Log.i(TAG, "onInitFail: 条码扫描仪初始化失败: " + errorCode.getCode() + " , " + errorCode.getMessage());
                    }
                });
                Toast.makeText(MyApplication.this,
                        "onInitializeFailed MRobot SDK 初始化失败" + errorCode.toString(),
                        Toast.LENGTH_SHORT).show();
                LogUtils.e(TAG, "MRobot onInitializeFailed" + errorCode.toString());
            }
        });
        //如果需要单独使用自己注册的Face++账号，请在这里进行相关配置。如果不配置VisionSystem的config，将默认使用SDK内部封装的apiKey和apiSecret
        initVision();
        initDbFile();
    }
    public static Context getContextObject(){
        return mContext;
    }
    public static MyApplication getInstance() {
        if (instance == null){
            synchronized (SYNC_LOCK){
                if (instance == null){
                    instance = new MyApplication();
                }
            }
        }
        return instance;
    }
    private void initVision() {
        //配置视觉系统用到的face++ apiKey和apiSecret
        //不配置的情况下,将使用SDK内部默认的apiKey和apiSecret
//        VisionSystem.get().config(this, FACE_APP_KEY, FACE_APP_SECRET);
    }

    private void initDbFile() {
        //copy db
        byte[] buffer = new byte[1024];
        OutputStream myOutput = null;
        int length;
        // Open your local db as the input stream
        InputStream myInput = null;
        try {
            myInput = getAssets().open(DB_NAME);
            // transfer bytes from the inputfile to the
            // outputfile
            String path = getDatabasePath(DB_NAME).getAbsolutePath();
            File file = new File(path);
            if (!file.exists()) {
                file.getParentFile().mkdirs();
                file.createNewFile();
            }
            myOutput = new FileOutputStream(path);
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }
            myOutput.close();
            myOutput.flush();
            myInput.close();
            Log.i("Database",
                    "New database has been copied to device!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
